package com.wsd.smartcarwasher.util;


import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.UnhandledException;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.io.UnsupportedEncodingException;
import java.security.Security;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;


//脑瘫微信支付
public class NTWXZF {
    /**
     * 微信退款通知解码
     *  <p>
     *  解密步骤如下：
     * （1）对加密串A做base64解码，得到加密串B
     * （2）对商户key做md5，得到32位小写key* ( key设置路径：微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置 )
     * （3）用key*对加密串B做AES-256-ECB解密（PKCS7Padding）
     * @param reqInfoSecret
     * @param key
     * @return
     */

    /**
     * AES-256-ECB(PKCS7Padding)解密
     * @param secretInfo 秘文
     * @param rawKey 商户的秘钥
     */
    public static String aesDecrypt(String secretInfo, String rawKey) throws UnhandledException {
        try {
            SecretKeySpec key = new SecretKeySpec(DigestUtils.md5Hex(getContentBytes(rawKey, "utf-8")).toLowerCase().getBytes(), "AES");
            Security.addProvider(new BouncyCastleProvider());
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "BC");
            cipher.init(Cipher.DECRYPT_MODE, key);
            return new String(cipher.doFinal(Base64.getDecoder().decode(secretInfo)));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static byte[] getContentBytes(String content, String charset) {
        if (charset == null || "".equals(charset)) {
            return content.getBytes();
        }
        try {
            return content.getBytes(charset);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("MD5签名过程中出现错误" + charset);
        }
    }



    /**
     * 解密测试
     * @param args
     */
    public static void main(String[] args) {
        String A = "3b+h9577VMEJ/zMd8+Evd66EMY0qIxoPxBTEIvPNRpsvsvaUSjCqQLC4JpHe4agsp8sfgSpWEGihr4/kgA6bM8ESiby/ojsRXmJ/Cq9g3T97tBY01qonr935a8yDUtknFEhyou2Mn1zvdcSrgTAgDuMgGFhsPkIhd1PySAD1TdDFawDfCztRQ32tljURnZtk5QOaZP8PveoJCh27p8BBwDV6Vuz/Ve4zvk6u5GdY5OfKuUOqCtrJhaq3a3sstkTLHoPnBWnUjziiVyOJwsd7uYFw6onfI8fX6Uv7qSyPmf/HyHFvNcC9ZN/zrMS+8xKX3wU3qYUzQx7jMFBw43gH2TvUmfUATluyylU1LbMYNqsXtyapSUyOPMLI18koW4yScuPemsHuqusyzSFmumDO9fx3EChMEfXhy4Ep7wnWWcmpbamYrD3ySuoTJFkFLdd1UgA7soQDetN9DBfupXNQIHWRASWlgDKrZdi1BIJGmmgH57HxuCXTIqT8FEiwew9M4pw5Dv81YBiyPABPp1lLY4aezjNS84CuI+L0qwRra/Ryxdtf+MpKUrnLFp7sU8qr+74Ahkxejd7NkhVHPxfgxH2coC9EwnObjTitzaysx0M7BYnLC4z8cCksCYn1ZYSImJGa55sn95JvhLy7eGqvdZjT3BtjfL1EmkGGw6y2fFON89bnU8wzxZJdUWDsCPPWyShtp0/0s/YKRkpftG/52bMdXo9f/fbYq0Xdsg6IKggAsIzpwv1mIRRtiGgjJdM1cuEiBiFsjVHSayKWxRhxOhG5Gp7JXF3UsaclemvTcM9+8QL4jXBWOh8g8myo0Evn6a5Xt3sKCKAYT2tACCAvxvZDA3lAbCcHxr+vLTfyzBZqxwR0I3YkrtyJ6DaRzDMsD5tndaCJTkQvoqfr4t2E11TwIQ4AyUmhYWM7mr4zEFPpdjOTiHgnaIIbIMCKdV01SBdO44OvjQeS6/+HzF7kQy4FTNwANMUVzV8a9km9VbhFOFlTDprLU7nJOlmjw/gx0Ffuy5K8JvSKvrv1n2bFHXlHqROFnn2/SD85IcbnPQhK+8akq69pFoO/mWIfRNWsARByx5sgRyrctFQjAa41Kw==";
        String key = "woniuzhinengkejiyouxiangongsi000";
        System.out.println(A);
        System.out.println(key);
        String B = aesDecrypt(A, key);
        try {
            System.out.println(CommonUtil.parseXml(B));

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}